package com.veeder.datacollection.collector;

import java.io.IOException;
import java.util.logging.Logger;
import com.veeder.datacollection.config.StationConfig;
import com.veeder.datacollection.config.StationParam;
import com.veeder.datacollection.exception.ConfigurationException;
import com.veeder.datacollection.rs232.AOKE;

import com.veeder.datacollection.rs232.VRTLS;
import com.veeder.datacollection.rs232.VRTLS2;
import com.veeder.datacollection.rs232.VRTLS3;
import com.veeder.datacollection.rs232.VRTLS4;
import com.veeder.datacollection.rs232.response.BasicTankInventory;
import com.veeder.datacollection.rs232.response.TankResponseReport;


public  class ATGFetcherFactory {

	private static ATGFetcher atg;
	protected final Logger logger;

	public ATGFetcherFactory() throws ConfigurationException {
		this.logger = Logger.getLogger(this.getClass().getName());
		logger.setUseParentHandlers(true);
	}

	public static ATGFetcher getATG() throws IOException, ConfigurationException {
		if (atg != null) {
			return atg;
		}
		synchronized (ATGFetcherFactory.class) {
			if (atg == null) {
				StationParam param = StationConfig.loadParams();
				String consoleMode = param.getConsoleModel();
				switch (consoleMode.toLowerCase()) {
				case "tls-2":
					atg = new VRTLS2();
					break;
				case "tls-3":
					atg = new VRTLS3();
					break;
				case "tls-4":
					atg = new VRTLS4();
					break;
				case "aoke":
					atg = new AOKE();
					break;
				default:
					atg = new VRTLS();
					break;
				}
			}
			return atg;
		}
	}
	
	public static void main(String[] args) {
		try {
			ATGFetcher atg=ATGFetcherFactory.getATG();
			TankResponseReport<BasicTankInventory>  inventory = atg.getAllTankInventories();
			for(int i: inventory.getAllTanks()) {
				System.out.print(inventory.getResponse(i).toString());
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
